﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.Crc.PoJo.Model.WorkForm;
using Com.Crc.WFManage.BLL.WorkFormManage;
using Com.Crc.PoJo.Model.Resource;
using Com.Crc.ResManage.BLL.EquipManage;

namespace Com.Crc.WorkFormManage.Web.ADSLFTTPFTTH
{
    public partial class ADSLFTTPFTTH : System.Web.UI.Page
    {

        WFWLANManageBLL bll = new WFWLANManageBLL();
        SubDistrictManageBLL subdistrictManagebll = new SubDistrictManageBLL();
        ONUManageBLL onuManagerBLL = new ONUManageBLL();
        UMPASSPORTManagerBLL umPASSPORTManagerBLL = new UMPASSPORTManagerBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitControl();
            }

            string action = Request.QueryString["action"];
            if (action == "showAccountInfo")
            {
                showAccountInfo();
                Response.End();
            }
        }
        private void InitControl()
        {
            this.newInstal_type.Attributes.Add("onclick", "return switchSelect('newInstal_type_tr');");
            this.modify_type.Attributes.Add("onclick", "return switchSelect('modify_type_tr');");
            this.eplan_type.Attributes.Add("onclick", "return switchSelect('eplan_type_tr');");
            this.unInstall_type.Attributes.Add("onclick", "return switchSelect('unInstall_type_tr');");
            this.addresstype_e_n.Attributes.Add("onchange", "return showSelectONU('addresstype_e_n','非标地址');");
           
            this.AREA_SHI.Attributes.Add("onchange", "return showNexeInfo('AREA_SHI');");
            this.AREA_QU.Attributes.Add("onchange", "return showNexeInfo('AREA_QU');");
            //this.AREA_ZHENG.Attributes.Add("onchange", "return showNexeInfo('AREA_ZHENG');");
            //this.AREA_CUN.Attributes.Add("onchange", "return showNexeInfo('AREA_CUN');");
            //this.AREA_ZU.Attributes.Add("onchange", "return showNexeInfo('AREA_ZU');");
            this.AREA_LU.Attributes.Add("onchange", "return showNexeInfo('AREA_LU');");
            this.AREA_SUBDISTICT.Attributes.Add("onchange", "return showNexeInfo('AREA_SUBDISTICT');");
           
            this.adsl.Attributes.Add("onclick", "return switchSelect('adsl');");
            this.epon.Attributes.Add("onclick", "return switchSelect('epon');");

            InitControlHelp.LoadBranchInfo(this.intypeddlOffice_a_n);//局点
            InitControlHelp.LoadCommonDicInfo(this.account_daikuan_e_n, Dictionary.COMMON_DIC_TYPE.WF_WLAN_DAIKUAN);


            InitControlHelp.LoadCommonDicInfo(this.addresstype_e_n, Dictionary.COMMON_DIC_TYPE.WF_WLAN_ADDRESS_TYPE);
            
            InitControlHelp.loadSubDistrict(this.inXincun_e_n);

            InitControlHelp.loadONU(this.dropdownoun_e_n);

            InitControlHelp.loadArea(this.AREA_SHI, Dictionary.COMMON_DIC_TYPE.AREA_SHI);
            InitControlHelp.loadArea(this.AREA_QU, Dictionary.COMMON_DIC_TYPE.AREA_QU);
            //InitControlHelp.loadArea(this.AREA_ZHENG, Dictionary.COMMON_DIC_TYPE.AREA_ZHENG);
            //InitControlHelp.loadArea(this.AREA_CUN, Dictionary.COMMON_DIC_TYPE.AREA_CUN);
            //InitControlHelp.loadArea(this.AREA_ZU, Dictionary.COMMON_DIC_TYPE.AREA_ZU);
            InitControlHelp.loadArea(this.AREA_LU, Dictionary.COMMON_DIC_TYPE.AREA_LU);
            InitControlHelp.loadSubdistictInfo(this.AREA_SUBDISTICT);
            InitControlHelp.loadSubdistictUnitInfo(this.ADDRESS_UNIT);
            
            
        }

        private void writeInfo(String msg)
        {
            if(newInstal_type.Checked)
            {
                if (adsl.Checked){
                    Response.Write("<script> var msg='" + msg + "';var selecttype = 'adsl';</script>");
                }else if(epon.Checked){
                    Response.Write("<script> var  msg='" + msg + "';var selecttype = 'epon';</script>");
                }
                
            }else if(modify_type.Checked){
                Response.Write("<script> var msg='" + msg + "';var selecttype ='modify_type_tr';</script>");
            }
            else if(eplan_type.Checked){
                Response.Write("<script> var msg='" + msg + "';var selecttype ='eplan_type_tr';</script>");
            }
            else if (unInstall_type.Checked) {
                Response.Write("<script> var msg='" + msg + "';var selecttype ='unInstall_type_tr'   ;</script>");
            }
        }


        protected void newInstall(object sender, EventArgs e)
        {
            WFWLAN data = new WFWLAN();

            data.S_BAK = bak.Text;
            bool isT = false;
            int port = -1;
            if (newInstal_type.Checked)
            {
                //分配端口

                port = onuManagerBLL.usePort(int.Parse(this.dropdownoun_e_n.SelectedValue), int.Parse(this.ADDRESS_UNIT.SelectedValue));

                if (port == -1)
                {
                    //自动分配设备端口已经被占用完
                    return;
                }

                
                data = setWorkFormValue();
                data.N_PORT = port;
                if (adsl.Checked)
                {
                    data.N_STATION_ID = int.Parse(this.intypeddlOffice_a_n.SelectedValue);
                    data.N_TYPE = WFWLAN.N_TYPE_ADSL;

                }
                else if (epon.Checked)
                {
                    data.N_IN_AREA = int.Parse(this.inXincun_e_n.SelectedValue);
                    data.N_TYPE = WFWLAN.N_TYPE_EPON;
                }
                else
                {
                    writeInfo(InitControlHelp.getErrorInfo(7, "新装工单选择错误"));
                    return;
                }
                UM_PASSPORT passport = setUmPassport(data);
                bool is1 = umPASSPORTManagerBLL.isexist(passport.S_ACCOUNT);
                if (is1)
                {
                    isT = umPASSPORTManagerBLL.modify(passport);
                }else{
                    isT = umPASSPORTManagerBLL.add(passport);
                }

                if (!isT)
                {
                    writeInfo(InitControlHelp.getErrorInfo(6, "帐号资料" + (is1 ? "修改" : "保存") + "失败，工单建立不成功!"));
                    return;
                }

                isT = bll.addWFWLAN(data);
                if (!isT)
                {
                    umPASSPORTManagerBLL.remove(passport.S_ACCOUNT);
                    writeInfo(InitControlHelp.getErrorInfo(5, "工单建立不成功!"));
                    return;
                }
            }
            else
            {
                data = setWorkFormValue();
                data.N_PORT = port;
                UM_PASSPORT passport = setUmPassport(data);;
                if (modify_type.Checked)
                {
                    data.N_TYPE = WFWLAN.N_TYPE_MODIFY;
                }
                else if (eplan_type.Checked)
                {
                    data.N_TYPE = WFWLAN.N_TYPE_EPLAN;
                    passport.S_ACCOUNT = newaccount_e_n.Text;
                    passport.S_ORG_ACCOUNT = data.S_ACCOUNT;
                }
                else if (unInstall_type.Checked)
                {
                    data.N_TYPE = WFWLAN.N_TYPE_UNINSTALL;
                }

                if (eplan_type.Checked){
                    isT = umPASSPORTManagerBLL.modifyAccount(passport);
                    if (!isT)
                    {
                        writeInfo(InitControlHelp.getErrorInfo(4, "修改帐号不成功!"));
                        return;
                    }
                }

                isT = umPASSPORTManagerBLL.modify(passport);
                if (!isT)
                {
                    writeInfo(InitControlHelp.getErrorInfo(3, "修改帐号资料不成功!"));
                    return;
                }

                isT = bll.addWFWLAN(data);
                if (!isT)
                {
                    writeInfo(InitControlHelp.getErrorInfo(2, "工单建立不成功!"));
                    return;
                }
            }
            writeInfo(InitControlHelp.getErrorInfo(1, "工单建立成功!"));
        }
        protected UM_PASSPORT setUmPassport(WFWLAN data)
        {
            UM_PASSPORT passport = new UM_PASSPORT();
            passport.S_ACCOUNT = data.S_ACCOUNT;
            passport.N_PORT = data.N_PORT;
            passport.N_STATION_ID = data.N_STATION_ID;
            passport.S_DK = data.S_DK;
            passport.S_NAME = data.S_NAME;
            passport.S_TEL = data.S_TEL;
            passport.S_ADDRESS_TYPE = data.S_ADDRESS_TYPE;
            passport.S_ADDRESS = data.S_ADDRESS;
            passport.N_IN_AREA = data.N_IN_AREA;
            passport.N_AREA_ID = data.N_AREA_ID;
            return passport;
        }
        protected WFWLAN setWorkFormValue()
        {
            WFWLAN data = new WFWLAN();
            data.S_TO_UPDATE = this.isGaizhuangEPON_e_n.Checked ? "1" : "0";
            data.S_ACCOUNT = this.account_e_n.Text.Trim();
            data.S_DK = this.account_daikuan_e_n.SelectedValue;
            data.S_NAME = this.accountname_e_n.Text.Trim();
            data.S_TEL = this.accountPhone_e_n.Text.Trim();
            data.S_ADDRESS_TYPE = this.addresstype_e_n.SelectedValue;
            if (data.S_ADDRESS_TYPE.Equals("3"))//标准地址
            {
                data.S_ADDRESS = this.ADDRESS_UNIT.SelectedValue;
            }
            else if (data.S_ADDRESS_TYPE.Equals("4"))//非标地址
            {
                data.S_ADDRESS = this.address_e_n.Text;
            }
            string[] arr = this.AREA_SUBDISTICT.SelectedValue.Split(new Char[] { '|' });
            data.N_IN_AREA = int.Parse(arr[arr.Length - 1].ToString());
            arr = this.AREA_LU.SelectedValue.Split(new Char[] { '|' });
            data.N_AREA_ID = int.Parse(arr[arr.Length - 1].ToString());
            data.S_BAK = this.bak.Text;
            return data;
        }
        
        protected String showAccountInfo() 
        {
            string account = Request.QueryString["account"];
            string data = umPASSPORTManagerBLL.queryAccountInfo(account);
            if (data == null)
            {
                Response.Write(responseFail("没有找到对应的帐号资料！",1000));
            }else{
                Response.Write(responseSucc(data));
            }
            
            Response.Flush();
            return null;
        }

        protected String responseSucc(String data)
        {
            return responseFail(data,1);
        }
        protected String responseFail(String data,int msgcode)
        {
            return "{\"msgcode\":\"" + msgcode + "\",\"msginfo\":" + data + "}";
        }

    }
}
